package com.zeng.controller;


import com.zeng.dao.LeagueTableQueryDao;
import com.zeng.entity.Result;
import com.zeng.entity.ResultPage;

import com.zeng.entity.Comments;
import com.zeng.service.CommentsService;
import com.zeng.utils.TimeUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * (Comments)表控制层
 *
 * @author zengfanchao
 * @since 2021-04-11 16:43:03
 */
@RestController
@RequestMapping("comments")
public class CommentsController {
    /**
     * 服务对象
     */
    @Resource
    private CommentsService commentsService;
    @Resource
    private LeagueTableQueryDao leagueTableQueryDao;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Comments selectOne(Integer id) {
        return this.commentsService.queryById(id);
    }

    @GetMapping("selectSellerCommentByUId")
    public List<HashMap<String, Object>> selectSellerCommentByUId(Integer id) {
        return this.leagueTableQueryDao.selectSellerCommentByUId(id);
    }

    /**
     * 查询全部
     *
     * @return
     */
    @GetMapping("selectAll")
    public List<Comments> selectAll() {
        return this.commentsService.selectAll();
    }

    /**
     * 根据页号查询
     *
     * @param offset 查询初始位置
     * @param limit  查询数量
     * @return
     */
    @GetMapping("selectAllByLimit")
    public ResultPage selectAllByLimit(int offset, int limit) {
        int pageTotal = this.commentsService.queryCount();
        List<Comments> list = this.commentsService.queryAllByLimit(offset, limit);
        return ResultPage.success(pageTotal, "查询成功", list);
    }

    @PostMapping("selectKeyByLimit")
    public ResultPage selectKeyByLimit(@RequestBody HashMap<String, String> map) {
        if (map.get("key").equals("")) {
            int pageTotal = this.commentsService.queryCount();
            List<Comments> list = this.commentsService.queryAllByLimit(Integer.valueOf(map.get("offset")), Integer.valueOf(map.get("limit")));
            return ResultPage.success(pageTotal, "查询成功", list);
        } else {
            int pageTotal = this.commentsService.queryCountSelect(map.get("key"));
            List<Comments> list = this.commentsService.queryKeyByLimit(map.get("key"), Integer.valueOf(map.get("offset")), Integer.valueOf(map.get("limit")));
            return ResultPage.success(pageTotal, "查询成功", list);
        }
    }

    /**
     * 根据主键删除
     *
     * @return
     */
    @GetMapping("deleteById")
    public Result deleteById(Integer id) {
        this.commentsService.deleteById(id);
        return Result.success("删除成功");
    }

    @GetMapping("deleteByFlag")
    public Result deleteByFlag(Integer id) {
        Comments comments = commentsService.queryById(id);
        comments.setDeleteflag(1);
        commentsService.update(comments);
        return Result.success("删除成功");
    }

    @RequestMapping("edit")
    public Result edit(@RequestBody HashMap<String, String> map
    ) {
        Comments comments = this.commentsService.queryById(Integer.valueOf(map.get("cid")));
        comments.setUid(Integer.valueOf(map.get("uid")));
        comments.setOrderid(Integer.valueOf(map.get("orderid")));
        comments.setDetails(map.get("details"));
        comments.setCreatetime(TimeUtils.NowTimeN());
        comments.setAnonymousflag(Integer.valueOf(map.get("anonymousflag")));
        comments.setDeleteflag(Integer.valueOf(map.get("deleteflag")));
        commentsService.update(comments);
        return Result.success("修改成功");
    }

    @RequestMapping("add")
    public Result add(@RequestBody HashMap<String, String> map
    ) {
        Comments comments = new Comments();
        comments.setUid(Integer.valueOf(map.get("uid")));
        comments.setOrderid(Integer.valueOf(map.get("orderid")));
        comments.setDetails(map.get("details"));
        comments.setCreatetime(TimeUtils.NowTimeN());
        comments.setAnonymousflag(Integer.valueOf(map.get("anonymousflag")));
        comments.setDeleteflag(Integer.valueOf(map.get("deleteflag")));
        commentsService.insert(comments);
        return Result.success("添加成功");
    }

    @RequestMapping("addpinglun")
    public Result addpinglun(@RequestBody HashMap<String, String> map
    ) {
        Comments comments = new Comments();
        comments.setOrderid(Integer.valueOf(map.get("orderid")));
        if (this.commentsService.queryAll(comments).size() > 0)
            return Result.error("此订单已经评论，无法再次评论");
        comments.setUid(Integer.valueOf(map.get("uid")));
        comments.setDetails(map.get("details"));
        comments.setCreatetime(TimeUtils.NowTimeN());
        comments.setAnonymousflag(Integer.valueOf(map.get("anonymousflag")));
        comments.setDeleteflag(0);
        commentsService.insert(comments);
        return Result.success("评论成功");
    }
}
